'''
Created on 2012-7-11

@author: wb_suoyao.shen
'''
from django import template
from django.template.defaultfilters import striptags
from django.utils.encoding import force_unicode

register = template.Library()

@register.filter(name='interceptwords')
def interceptwords(value,length,end_text = '...'):
    ''' intercept words '''
    val = striptags(value)
    val = force_unicode(val)
    s = u''
    index = 0
    leng = int(length)
    while leng > 0:
        char = val[index : index + 1]
        index += 1
        if char > u'\x80':
            leng -= 2
        else:
            leng -= 1
        s += char
    if s != val:
        s += end_text
    return s
